package com.example.controller;

import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.example.common.R;
import com.example.model.req.UploadTransferReq;
import com.example.service.TransferService;
import com.example.utils.ImageUtil;

@ResponseBody
@RestController
@RequestMapping("/transfer")
public class TransferController {

    @Autowired
    private TransferService transferService;
    @Autowired
    private ImageUtil imageUtil;

    @PostMapping("/add")
    public R<String> uploadTransfer(@RequestParam("orderId") Integer orderId, @RequestParam("amount") BigDecimal amount,
            @RequestParam("transferPic") MultipartFile transferPic, @RequestParam("fromTo") Integer fromTo,
            @RequestParam("transferTo") Integer transferTo) throws IOException {

        String fileName = transferPic.getOriginalFilename();
        String imgName = imageUtil.getRandomImgName(fileName);

        FileInputStream inputStream = (FileInputStream) transferPic.getInputStream();
        String path = imageUtil.uploadImageQiniu(inputStream, imgName);

        return transferService.uploadTransfer(UploadTransferReq.builder().orderId(orderId).amount(amount).fromTo(fromTo)
                .transferTo(transferTo).transferPic(path).build());
    }
}
